package com.wolfking.controller;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.wolfking.converter.enums.ConverterType;
import com.wolfking.converter.rowmapper.AbstractFieldRowMapper;
import com.wolfking.converter.service.JdbcTemplateService;
import com.wolfking.converter.transform.extend.StringSplitListTransform;
import com.wolfking.converter.util.JsonConverterUtil;
import com.wolfking.demo.converter.A;
import com.wolfking.demo.converter.DemoInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/assembly")
@Api(value = "assembly", tags = "assembly生成对象")
public class AssemblyObjectController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JdbcTemplateService jdbcTemplateService;

    @Autowired
    private DemoInterface demoInterface;

    @SneakyThrows
    @GetMapping("/assemblyFromJdbc")
    @ApiOperation(value = "assemblyFromJdbc")
    public Object assemblyFromJdbc() {
        List<A> list = Lists.newArrayList();

        A a1 = demoInterface.queryOne();
        list.add(a1);

        List<A> as = demoInterface.queryList();
        list.addAll(as);

        a1 = demoInterface.queryOneArgs("zhaoweiwei", "男", "xi,men,qing");
        list.add(a1);

        a1 = demoInterface.queryOneArgsParma("zhao#wei#wei");
        list.add(a1);

        a1 = demoInterface.queryOneArgsParma2("zhao,wei,wei", "男", "aaa,bbb");
        list.add(a1);

        List<A> as1 = demoInterface.queryListIds(Lists.newArrayList(1L, 2L, 3L, 4L, 5L));
        list.addAll(as1);

        /*通用*/
        String sql = "select 'ximenqing' as `name`,'女' as `sex.sex`,'100' as `sex.age`,1000 as `salary`,now() as `birth`,'a,b,c,d,e' as `jobs`," +
                "'chars' as chars,'bytes' as bytes ,'truE' as `b` from student limit 1";
        A a = jdbcTemplateService.queryOne("", sql, A.class, ImmutableMap.of("jobs", new StringSplitListTransform()), ConverterType.FIELD);
        list.add(a);

        sql = "select 'xudong' as `name`,'男' as `sex.sex`,'99' as `sex.age`,'10000' as `salary`,now() as `birth` from student";
        List<A> query = jdbcTemplateService.queryList("", sql, A.class);
        list.addAll(query);


//        /*原生*/
//        sql = "select 'xudong' as `name`,'男' as `sex.sex`,'99' as `sex.age`,'10000' as `salary`,now() as `birth`";
//        A b = jdbcTemplateService.assemblyObject(jdbcTemplate.queryForMap(sql), A.class);
//        list.add(b);
//
//        sql = "select 'xudong' as `name`,'男' as `sex.sex`,'99' as `sex.age`,'10000' as `salary`,now() as `birth` from student";
//        query = jdbcTemplateService.assemblyObjectList(jdbcTemplate.queryForList(sql), A.class);
//        list.addAll(query);

        /*自定义*/
        sql = "select 'xudong' as `name`,'男' as `sex.sex`,'99' as `sex.age`,'10000' as `salary`,now() as `birth` from student";
        query = jdbcTemplate.query(sql, new AbstractFieldRowMapper<A>() {
        });
        list.addAll(query);

        return list;
    }

    @SneakyThrows
    @GetMapping("/assemblyFromJdbcObject")
    @ApiOperation(value = "assemblyFromJdbcObject")
    public Object assemblyFromJdbcObject() {
        String sql = "select *,create_time as `c.c.c.c`,num as `a.b` from student limit 1";
        return JsonConverterUtil.toJson(jdbcTemplateService.queryObject("",sql));
    }

    @SneakyThrows
    @GetMapping("/assemblyFromJdbcObjectBind")
    @ApiOperation(value = "assemblyFromJdbcObjectBind")
    public Object assemblyFromJdbcObjectBind() {
        String sql = "select *,DATE_FORMAT(update_time,'%m-%d %H:%i') as `a.update_time`,num as `a.b` from student where id = :id";
        return JsonConverterUtil.toJson(jdbcTemplateService.queryObject("",sql, ImmutableMap.of("id", 1)));
    }

    @SneakyThrows
    @GetMapping("/assemblyFromJdbcArray")
    @ApiOperation(value = "assemblyFromJdbcArray")
    public Object assemblyFromJdbcArray() {
        String sql = "select *,IFNULL(name,'ximenqina') as `cname` from student";
        JsonArray array = jdbcTemplateService.queryArray("",sql);
        return JsonConverterUtil.toJson(array);
    }

    @SneakyThrows
    @GetMapping("/assemblyFromJdbcArrayBind")
    @ApiOperation(value = "assemblyFromJdbcArrayBind")
    public Object assemblyFromJdbcArrayBind() {
        String sql = "select *,#{name} as `c.c.c.c`,num as `a.b` from student";
        JsonArray array = jdbcTemplateService.queryArray("",sql, ImmutableMap.of("name", "nihao"));
        return JsonConverterUtil.toJson(array);
    }
}
